package com.happyfamily.springboot.crowdsourcingplatform.controller.wang;

import com.happyfamily.springboot.crowdsourcingplatform.annotation.OperationLogDetail;
import com.happyfamily.springboot.crowdsourcingplatform.base.result.PageTableRequest;
import com.happyfamily.springboot.crowdsourcingplatform.base.result.Results;
import com.happyfamily.springboot.crowdsourcingplatform.enums.OperationType;
import com.happyfamily.springboot.crowdsourcingplatform.enums.OperationUnit;
import com.happyfamily.springboot.crowdsourcingplatform.model.Work;
import com.happyfamily.springboot.crowdsourcingplatform.service.wang.VerifyWorkService;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.io.OutputStream;

/**
 * 后台作品管理的控制类
 */
@Controller
@RequestMapping("/api-back/verifyWork")
@Slf4j
public class VerifyWorkController
{

	@Autowired
	private VerifyWorkService verifyWorkServiceImpl;

	/**
	 * 查看作品表
	 *
	 * @param request
	 * @param workName
	 * @param starttime
	 * @param endtime
	 * @param verifyStatus
	 * @return
	 */
	@PreAuthorize("hasAnyAuthority('back:work:query')")
	@ApiOperation(value = "管理员查看作品表", notes = "管理员查看作品表")
	@OperationLogDetail(detail = "管理员查看作品表", level = 1, operationUnit = OperationUnit.ADMIN, operationType = OperationType.SELECT)
	@RequestMapping("/workDeploy")
	@ResponseBody
	public Results<Work> workDeploy(PageTableRequest request, String workName, String starttime, String endtime, String verifyStatus)
	{
		request.countOffset();
		return verifyWorkServiceImpl.workDeploy(request.getOffset(), request.getLimit(), workName, starttime, endtime, verifyStatus);
	}

	/**
	 * 作品审核通过
	 * @param wid
	 * @return
	 */
	@PreAuthorize("hasAnyAuthority('back:work:yes')")
	@ApiOperation(value = "管理员对作品审核通过", notes = "管理员对作品审核通过")
	@OperationLogDetail(detail = "管理员对作品审核通过", level = 2, operationUnit = OperationUnit.ADMIN, operationType = OperationType.UPDATE)
	@PostMapping("/work-yes")
	@ResponseBody
	public String workYes(String wid)
	{
		return verifyWorkServiceImpl.workYes(wid);
	}

	/**
	 * 作品审核不通过
	 * @param wid
	 * @param fname
	 * @param request
	 * @return
	 */
	@PreAuthorize("hasAnyAuthority('back:work:no')")
	@ApiOperation(value = "管理员对作品审核不通过", notes = "管理员对作品审核不通过")
	@OperationLogDetail(detail = "管理员对作品审核不通过", level = 2, operationUnit = OperationUnit.ADMIN, operationType = OperationType.UPDATE)
	@PostMapping("/work-no")
	@ResponseBody
	public String workNo(String wid, String fname,String wuseraccount)
	{
		return verifyWorkServiceImpl.workNo(wid, fname, wuseraccount);
	}

	/**
	 * 查看作品详情
	 * @param wid
	 * @return
	 */
	@PreAuthorize("hasAnyAuthority('back:work:details')")
	@ApiOperation(value = "管理员对作品查看详情", notes = "管理员对作品查看详情")
	@OperationLogDetail(detail = "管理员对作品查看详情", level = 1, operationUnit = OperationUnit.ADMIN, operationType = OperationType.SELECT)
	@PostMapping("/work-details")
	@ResponseBody
	public Work workDetails(String wid)
	{
		return verifyWorkServiceImpl.workDetails(wid);
	}


	/**
	 * 进行作品记录删除
	 * @param wid
	 * @return
	 */
	@PreAuthorize("hasAnyAuthority('back:work:delete')")
	@ApiOperation(value = "管理员对作品记录进行删除", notes = "管理员对作品记录进行删除")
	@OperationLogDetail(detail = "管理员对作品记录进行删除", level = 4, operationUnit = OperationUnit.ADMIN, operationType = OperationType.DELETE)
	@PostMapping("/work-delete")
	@ResponseBody
	public String workDelete(String wid)
	{
		return verifyWorkServiceImpl.workDetele(wid);
	}

	/**
	 * 进行作品下载
	 * @param wid
	 * @return
	 */
	@ApiOperation(value = "管理员对作品下载", notes = "管理员对作品下载")
	@RequestMapping("/work-download")
	public void workDownload(String fname, String wuseraccount, HttpServletResponse response)throws Exception
	{
		File f= new File("src/main/resources/doc");
		File file=new File(f.getAbsolutePath()+"/"+wuseraccount+"/"+fname);
		//设置Content-Disposition
		response.setHeader("Content-Disposition", "attachment; filename="+new String(fname.getBytes(),"iso8859-1"));
		//读取文件
		InputStream in = new FileInputStream(file);
		OutputStream out = response.getOutputStream();
		//写文件
		int b;
		while((b=in.read())!= -1)
		{
			out.write(b);
		}
		in.close();
		out.close();
	}

}
